/**
 * LinkedLST, Kellen Donohue
 * May 20, 2011
 * DataQueryTestCase.java - Tests that DataQuery methods work properly, these also test DataGet
 */
package edu.washington.cs.labspecimentransport.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.conn.ConnectTimeoutException;

import edu.washington.cs.labspecimentransport.data.*;
import edu.washington.cs.labspecimentransport.dataobjects.*;
import junit.framework.TestCase;

/**
 * @author Kellen
 * 
 */
public class DataQueryTestCase extends TestCase {
	private DataQuery dq;

	public DataQueryTestCase() throws ConnectTimeoutException {
		dq = DataQuery.getInstance();
		assertTrue(dq.authenticate("jsmith", "password"));
	}

	public void testGetCheckList() throws ConnectTimeoutException {
		List<ChecklistItem> face = dq.getBeforeChecklist();
		assertEquals("Turn in Facebook.", face.get(0).getTask_text());

		face = dq.getAfterChecklist();
		assertEquals("fixit.sh", face.get(2).getTask_text());
	}

	public void testBarcodeByItemId() throws ConnectTimeoutException {
		assertEquals("LVChepHwRPMd3", dq.getBarcodesByItemId(559752675).get(0)
				.getIdentifier_value());
	}

	public void testGetItemById() throws ConnectTimeoutException {
		Item i = dq.getItemById(64810937);
		assertEquals(64810937, (int) i.getClient_id());
		assertEquals(64810937, (int) i.getId());
		assertEquals(455239745, (int) i.getItem_type_id());
	}

	public void testCheckIn() throws ConnectTimeoutException {

		Item i = new Item();
		i.set_id(dq.getItemIdByBarcode("0001234567895"));
		List<Integer> items = new ArrayList<Integer>();
		items.add(i.getId());
		assertEquals("Success",
				dq.markItemsAsPickedUp(135138680, 93868891, items));

		// Make sure the item was picked up
		assertTrue(dq.getItemById(i.getId()).getPicked_up());
	}

	public void testGetBagContentsByBarcode() throws ConnectTimeoutException {
		Map<ItemAttribute, Integer> obj = dq.getCountOfColorsForBag("12345678");
		for (ItemAttribute o : obj.keySet()) {
			System.out.println(o.getDisplay_text() + ":" + obj.get(o));
		}
		// Make sure we didn't fail
		assertNotNull(obj);
	}

	public void testGetCourierStops() throws ConnectTimeoutException {
		List<Location> stops = dq.getLocationsByCourierId(64810937);
		for (Location l : stops) {
			System.out.println(l.getName());
		}
		assertTrue(stops.size() > 0);
	}

	public void testSetStopInstanceVisited() throws ConnectTimeoutException {
		StopInstance si = dq.getStopInstancesByCourierId(64810937).get(0);
		si.set_visited(true);

		si = dq.markLocationAsVisited(si);
		StopInstance si2 = dq.getStopInstancesByCourierId(64810937).get(0);
		// assertEquals(si.getId(), si2.getId());
		assertTrue(si2.getVisited());
	}

	public void testGetUserName() throws ConnectTimeoutException {
		String ui = dq.getUserNameById(135138680);
		assertEquals("Mister Admin", ui);
	}
	
	public void testCurrentLoggedInUser() throws ConnectTimeoutException {
		Courier c = dq.getCurrentCourier();
		assertEquals(98190962, (int)c.getId());
	}

	public void testGetAttributes() throws ConnectTimeoutException {
		boolean seenRefrigerated = false;
		boolean seenRedCap = false;
		List<ItemAttribute> attrs = dq.getAttributesByItemId(64810937);
		for (ItemAttribute attr : attrs) {
			if (attr.getDisplay_text().equals("Red")) {
				seenRedCap = true;
			} else if (attr.getDisplay_text().equals("Refrigerated")) {
				seenRefrigerated = true;
			}
		}

		assertTrue(seenRedCap);
		assertTrue(seenRefrigerated);
	}

	public void testMarkItemLost() {
		// re-enable after marking items as lost is working
		// assertTrue(dq.markItemAsLost(64810937, "Dunno it went somewhere"));
	}

	public void testGetItemColor() throws ConnectTimeoutException {
		ItemAttribute color = dq.getItemCapColor(64810937);
		assertEquals("Red", color.getDisplay_text());
	}

	public void testBagItemsGetter() throws ConnectTimeoutException {
		List<Item> bag = dq.getItemsByBag(64810937);
		Item bagContents = bag.get(0);
		assertEquals((int) 288870769, (int) bagContents.getId());
	}

	public void testGetItemTypeById() throws ConnectTimeoutException {
		ItemType t = dq.getItemTypeById(429865220);
		assertEquals("Specimen", t.getItem_type());
	}

	public void testGetLocationById() throws ConnectTimeoutException {
		Location l = dq.getLocationFromId(93868891);
		assertEquals("Harborview Medical Center", l.getName());
	}

	public void testEnsurePickupableItems() {/*
											 * List<Item> allItemsAtLocation =
											 * dq.getItemsByLocation(598124354,
											 * 93868891);
											 * 
											 * // filter out items that have
											 * already been picked up for (Item
											 * i : allItemsAtLocation) { if
											 * (!i.getPicked_up()) { return; } }
											 * 
											 * //fail();
											 */
		// /*
	}

	public void testGetLoggedInCourier() throws ConnectTimeoutException {
		Courier c = dq.getCurrentCourier();
		assertNotNull(c);
	}

	public void testSendCourierDataUpdate() throws ConnectTimeoutException {
		// Send an update
		int newLat = (int) (Math.random() * 100 - 50);
		int newLong = (int) (Math.random() * 100 - 50);
		dq.sendCurrentLatLong(newLat, newLong);

		// Now make sure the update worked
		Courier c = dq.getCurrentCourier();
		assertEquals(newLat, (int) c.getLatest_lat());
		assertEquals(newLong, (int) c.getLatest_long());
	}
}
